package com.szholly.plug.safe.entity;

import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.*;
import com.szholly.data.general.metadata.IBaseTable;

/**
 * 定义系统日志表
 */
@Component("SAFE_LOG")
public class LogService extends InterceptorTableBase implements IBaseTable {

	public final static String TABLE_NAME = "SAFE_LOG";
	
	/**
	 * 获取单例对象
	 */
	public static LogService getSingleRef() {
		return (LogService) SpringBeanFactory.getBean("SAFE_LOG");
	}

	@Override
	public String getMetadataTableName() {
		return TABLE_NAME;
	}

	@Override
	public String getMetadataTableAliasName() {
		return "系统日志表";
	}

	private HashMap<String, Field> fields;

	@Override
	protected HashMap<String, Field> fields() {
		if (fields == null) {
			fields = new HashMap<String, Field>();

			Field itemField = new Field();

			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_VERSION);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("数据版本");
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_COMPANY);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("企业名称");
			itemField.setCanShow(false);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_SYSTEMNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("系统名称");
			itemField.setCanShow(false);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(LogEntity.FIELD_LOGID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(2);
			itemField.setIsKey(true);
			itemField.setFieldAliasName("日志主键");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(LogEntity.FIELD_USERNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("访问用户ID");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(LogEntity.FIELD_LOGTYPE);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(5);
			itemField.setFieldAliasName("日志类型");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			itemField.setDomain(LogTypeDic.getDomain());
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(LogEntity.FIELD_C_IP);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(7);
			itemField.setFieldAliasName("日志内容");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(LogEntity.FIELD_C_LOGDES);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(7);
			itemField.setFieldAliasName("日志内容");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(LogEntity.FIELD_D_LOGDATE);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(8);
			itemField.setFieldAliasName("日志记录时间");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

		}
		return fields;
	}

	/**
	 * 表分组信息
	 */
	@Override
	public String getTableGroup() {
		return "用户权限控制";
	}

	/**
	 * 表分组信息
	 */
	@Override
	public String getGUIDFieldName() {
		return LogEntity.FIELD_LOGID;
	}
	
	public void SaveEntityInThread(LogEntity entity){
		LogSaveThread logThread = new LogSaveThread(this, entity);
		logThread.start();
	}
	
	private class LogSaveThread extends Thread {

		private LogService logService;
		private LogEntity entity;
		
		public LogSaveThread(LogService logService, LogEntity entity){
			this.logService = logService;
			this.entity = entity;
		}
		
		@Override
		public void run() {
			logService.save(entity);
		}
	}
}